// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    list: [],
    all: 'all',
    last_page: 1,
    current_page: 1
  },
  onLoad: function (options) {
    this.setData({
      currentTab: options.currentTab
    })
    var that = this;
    // 判断传参是否为空
    if (Object.keys(options).length === 0) {
      this.setData({
        currentTab: 0
      })
    } else {
      this.setData({
        currentTab: options.currentTab
      })
    }

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });

    // 列表
    let openid = wx.getStorageSync("openid")
    // 是否有openid登录
    if (openid != '') {
      if (this.data.currentTab == 0) {
        this.list(this.data.all)
      } else {
        this.list(this.data.currentTab)
      }



    } else {
      wx.reLaunch({
        url: '/pages/login/login'
      })
    }

  },
  list(status) {
    var that = this
    wx.request({
      url: `${baseurl}/good.ExchangeShop/exchange_order`,
      data: {
        user_id: wx.getStorageSync("user_id"),
        type: 2,
        status: status
      },
      success: function (res) {
        // console.log("11111", res.data.data);
        that.setData({
          list: res.data.data.data,
          last_page: res.data.data.last_page,
          current_page: res.data.data.current_page
        })
      }
    })
  },

  //  tab切换逻辑
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (this.data.currentTab == 0) {
      this.list(this.data.all)
    } else {
      this.list(this.data.currentTab)
    }

  },

  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },

  // 触底加载
  getMore1(that, status) {
    var page = that.data.current_page; //页码
    if (that.data.last_page == page) {

      wx.showToast({
        title: '没有更多数据！',
        icon: 'none',
        duration: 2000
      })
    } else {
      page++;
      this.setData({
        current_page: page
      })
      // console.log('111111111111', page);
      // console.log(page);
      // 需求列表
      wx.request({
        url: `${baseurl}/good.ExchangeShop/exchange_order`,
        method: 'POST',
        data: {
          user_id: wx.getStorageSync("user_id"),
          type: 2,
          status: status,
          page: page, // 当前页
        },
        success: (res) => {
          this.setData({
            list: [...this.data.list, ...res.data.data.data]
          })
          // console.log(this.data.list);
        }
      })
      return false
    }

  },
  scrollbottom(e) {
    var that = this
    // console.log('page', that.data.current_page);
    // console.log('last_page', that.data.last_page);
    // if (that.data.last_page == that.data.current_page) {
      // wx.showToast({
      //   title: '没有更多数据！',
      //   icon: 'none',
      //   duration: 2000
      // })
    // } else {
      // console.log('22222222222222');
      var that = this;
      if (this.data.currentTab == 0) {
        this.getMore1(that, that.data.all)
      } else {
        this.getMore1(this.data.currentTab)
        this.getMore1(that, this.data.currentTab)
      }
    // }

  },
})